package com.example.stuasist.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.stuasist.db.stuasistDbOpenHelper;
import com.example.stuasist.domain.Course;

public class CourseDao {

	private stuasistDbOpenHelper dbOpenHelper;

	// 在CourseDao被new出来的时候 dbOpenHelper就完成初始化

	public CourseDao(Context context) {
		super();
		this.dbOpenHelper = new stuasistDbOpenHelper(context);
	}

	/**
	 * 増删改差
	 */

	// 增加数据

	public void add(String week_day, int day_no, String course_name,
			String time_detil, String classroom, String teacher_name,
			String weeks) {
			boolean result = find(week_day,day_no);
				if (result)
					return;

		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {

			db.execSQL(
					"insert into Schedule(week_day,day_no,course_name,time_detil,classroom,teacher_name,weeks) values(?,?,?,?,?,?,?)",
					new Object[] { week_day, day_no, course_name, time_detil,classroom, teacher_name, weeks });
			db.close();
		}

	}

	// 查找
	public boolean find(String week_day, int day_no) {

		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		boolean result = false;
		if (db.isOpen()) {
			
			 Cursor cursor =
			 db.rawQuery("select * from Schedule where week_day = ? and day_no = ? ",new
			 String[]{week_day,day_no+""});
		
			if (cursor.moveToFirst()) {
				//String phone = cursor.getString(cursor.getColumnIndex("phone"));
				return true;
			}
			cursor.close();
			db.close();
			return result;
		}
		return result;
	}

	// 删除
	public void delete(String week_day, int day_no) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {
			db.execSQL("delete from Schedule where week_day = ? and day_no = ?",new Object[]{week_day,day_no+""});
			//db.delete("person", "name=?", new String[] { name });
			db.close();
		}
	}

	// 更新一条数据
	public void update(String week_day, int day_no, String course_name,
			String time_detil, String classroom, String teacher_name,
			String weeks,String old_week_day, int old_day_no) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {
			
			 db.execSQL("update Schedule set week_day =?,day_no =?,course_name =?,time_detil =?,classroom =?,teacher_name =?,weeks =? where week_day = ? and day_no = ?",
			 new Object[]{week_day,day_no,course_name,time_detil,classroom,teacher_name,weeks,old_week_day,old_day_no});
	
			db.close();
		}

	}

	// 查找全部
	public List<Course> getAllCourses() {
		List<Course> courses = null;
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		if (db.isOpen()) {
			courses = new ArrayList<Course>();
			//Cursor cursor = db.query("person", null, null, null, null, null,null);
			 Cursor cursor = db.rawQuery("select * from Schedule",null);
			while (cursor.moveToNext()) {
				Course course = new Course();
				
				int Index_week_day = cursor.getColumnIndex("week_day");
				int Index_day_no = cursor.getColumnIndex("day_no");
				int Index_course_name = cursor.getColumnIndex("course_name");
				int Index_time_detil = cursor.getColumnIndex("time_detil");
				int Index_classroom = cursor.getColumnIndex("classroom");
				int Index_teacher_name = cursor.getColumnIndex("teacher_name");
				int Index_weeks = cursor.getColumnIndex("weeks");
				
				String week_day = cursor.getString(Index_week_day);
				int day_no = cursor.getInt(Index_day_no);
				String course_name = cursor.getString(Index_course_name);
				String time_detil = cursor.getString(Index_time_detil);
				String classroom = cursor.getString(Index_classroom);
				String teacher_name = cursor.getString(Index_teacher_name);
				String weeks = cursor.getString(Index_weeks);
			
			    course.setWeek_day(week_day);
				course.setDay_no(day_no);
				course.setCourse_name(course_name);
				course.setTime_detil(time_detil);
				course.setClassroom(classroom);
				course.setTeacher_name(teacher_name);
				course.setWeeks(weeks);
				
				courses.add(course);
			}
			cursor.close();
			db.close();
		}

		return courses;
	}
	
	public void deleteAll() {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		
		if(db.isOpen()){
			
			db.execSQL("delete from Schedule");
			
			db.close();
		}
		
		
	}
}
